package com.object.module.cbb.service.impl;

import com.common.rabitmq.RabbitmqTime;
import com.object.configuration.rabbitmq.RabbitmqUtil;
import com.object.configuration.utils.PasswordEncryp;
import com.object.configuration.utils.PayUtile;
import com.object.configuration.utils.Q;
import com.object.module.cbb.entity.CbbShoppingCartEntity;
import com.object.module.cbb.entity.CbbUserEntity;
import com.object.module.cbb.entity.CddRechargeDetailsEntity;
import com.object.module.cbb.ov.PaymentPassword;
import com.object.module.cbb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private CddRechargeDetailsService rechargeDetailsService;
    @Autowired
    private CbbUserService userService;
    @Autowired
    private CbbShoppingCartService shoppingCartService;
    @Autowired
    private CddShopService shopService;

    @Autowired
    private RabbitmqUtil rabbitmqUtil;

    @Transactional
    @Override
    public Q getPaymentCode(CddRechargeDetailsEntity recharge) {
        //设置我们的订单流水号
        recharge.setRdUuid(UUID.randomUUID().toString().replace("-", ""));
        recharge.setRdCreateTime(new Date());
        //保存我们的预下单信息
        rechargeDetailsService.save(recharge);
        //获取我们的支付信息
        String url = PayUtile.getPay(recharge.getRdUuid(), recharge.getRdPrice(), "cbb充值", RabbitmqTime.Hour(1));
        return Q.ok().put("payUrl", url).put("uuid", recharge.getRdUuid());
    }

    @Transactional
    @Override
    public Q getPayStatus(CddRechargeDetailsEntity re) {
        Q q = Q.ok();
        String uuid = re.getRdUuid();
        Double success = PayUtile.getPaymentSuccess(uuid);
        if (success == null) {
            return q.message("等待扫码");
        }
        CddRechargeDetailsEntity entity = rechargeDetailsService.getById(uuid);
        if (entity.getRdStatus() == 2) {
            //证明已经支付成功了
            return q.message("支付成功");
        }
        if (entity.getRdPrice().equals(success)) {
            //支付成功
            entity.setRdStatus(2);
            //给用户添加钱包的余额
            Long urId = entity.getRdUrId();
            CbbUserEntity user = userService.getById(urId);
            double wallet = user.getUrWallet();
            double he = wallet + entity.getRdPrice();
            DecimalFormat df = new DecimalFormat("#0.00");
            double aDouble = Double.parseDouble(df.format(he));
            user.setUrWallet(aDouble);
            entity.setRdCountPrice(aDouble);
            userService.updateById(user);
            rechargeDetailsService.updateById(entity);
            return q.message("支付成功");
        } else {
            return q.message("支付中ing");
        }
    }

    //输入支付密码 就支付成功
    @Override
    @Transactional
    public Q PaymentPassword(List<PaymentPassword> paymentPasswords) {
        List<Integer> ids = new ArrayList<>();
        CbbUserEntity user = userService.getById(paymentPasswords.get(0).getUrId());
        //累计我们的需要支付的金额
        double counts = 0;
        paymentPasswords.forEach(item -> {
            ids.add(item.getCtId());
        });
        List<CbbShoppingCartEntity> shoppingCartEntities = shoppingCartService.listByIds(ids);
        for (CbbShoppingCartEntity shoppingCartEntity : shoppingCartEntities) {
            counts += shoppingCartEntity.getScCount() * shopService.getById(shoppingCartEntity.getScSpId()).getSpSellingPrice();
        }
        //判断一下支付密码是否正确
        if (PasswordEncryp.IfPassowrd(paymentPasswords.get(0).getPayPassword(), user.getUrPaymentPassword())) {
            //如果密码正确
            //判断余额是否足够
            double urWallet = user.getUrWallet();
            if (urWallet - counts >= 0) {
                //证明我们的余额足够买这个商品
                // 把任务放到我们的rabbitmq里面去让 其他业务去处理
                rabbitmqUtil.sendPaymentInformation(ids.toString());
                return Q.ok().message("下单成功");
            } else {
                return Q.error().message("余额不足");
            }
            //我们首先扣钱然后下订单

        }
        return Q.error().message("支付密码错误");
    }
}
